home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-09-22 | 8.6 KB | 398 lines |
- package com.symantec.itools.io;
-
-
- import java.io.File;
- import java.io.FilenameFilter;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import com.symantec.itools.util.ArrayUtils;
-
-
- /**
- * @author Symantec Internet Tools Division
- * @version 1.0
- * @since VCafe 3.0
- */
-
- public class Directory
- {
- /**
- * @since VCafe 3.0
- */
- protected String name;
-
- /**
- * @since VCafe 3.0
- */
- protected Directory parent;
-
- /**
- * @since VCafe 3.0
- */
- protected File directory;
-
- protected Directory()
- {
- }
-
- public Directory(String dirName)
- throws NotDirectoryException,
- FileNotFoundException,
- IOException
- {
- this(dirName, false);
- }
-
- public Directory(String dirName, boolean create)
- throws NotDirectoryException,
- FileNotFoundException,
- IOException
- {
- setName(dirName, create);
- }
-
- /**
- * @param dirName TODO
- * @param create TODO
- * @exception com.symantec.itools.io.NotDirectoryException
- * @exception java.io.FileNotFoundException
- * @exception java.io.IOException
- * @since VCafe 3.0
- */
-
- protected void setName(String dirName, boolean create)
- throws NotDirectoryException,
- FileNotFoundException,
- IOException
- {
- name = FileSystem.getCanonicalPath(dirName, true);
- directory = new File(name);
-
- if(directory.exists())
- {
- if(!(directory.isDirectory()))
- {
- throw new NotDirectoryException(name);
- }
- }
- else
- {
- if(create)
- {
- directory.mkdirs();
- }
- else
- {
- throw new FileNotFoundException(name);
- }
- }
- }
-
- /**
- * @since VCafe 3.0
- */
-
- public String getName()
- {
- return (name);
- }
-
- /**
- * @param recurse TODO
- * @since VCafe 3.0
- */
-
- public String[] listFiles(boolean recurse)
- {
- return (listFiles(new DefaultFileFilenameFilter(), recurse));
- }
-
- /**
- * @param filter TODO
- * @param recurse TODO
- * @since VCafe 3.0
- */
-
- public String[] listFiles(FileFilenameFilter filter, boolean recurse)
- {
- String[] list;
-
- list = list(filter, recurse);
-
- if(list == null)
- {
- list = new String[0];
- }
-
- return (list);
- }
-
- /**
- * @param recurse TODO
- * @since VCafe 3.0
- */
-
- public String[] listDirectories(boolean recurse)
- {
- return (listDirectories(new DefaultDirectoryFilenameFilter(), recurse));
- }
-
- /**
- * @param filter TODO
- * @param recurse TODO
- * @since VCafe 3.0
- */
-
- public String[] listDirectories(DirectoryFilenameFilter filter, boolean recurse)
- {
- String[] list;
-
- list = list(filter, recurse);
-
- if(list == null)
- {
- list = new String[0];
- }
-
- return (list);
- }
-
- /**
- * @param recurse TODO
- * @since VCafe 3.0
- */
-
- public String[] list(boolean recurse)
- {
- return (list((FilenameFilter)null, recurse));
- }
-
- /**
- * @param filter TODO
- * @param recurse TODO
- * @since VCafe 3.0
- */
-
- public String[] list(FilenameFilter filter, boolean recurse)
- {
- String[] list;
-
- if(directory == null)
- {
- return (null);
- }
-
- list = directory.list(filter);
-
- for(int i = 0; i < list.length; i++)
- {
- list[i] = (new File(name + list[i])).getAbsolutePath();
- }
-
- if(recurse)
- {
- String[] directories;
-
- directories = listDirectories(false);
-
- for(int i = 0; i < directories.length; i++)
- {
- try
- {
- String[] dirs;
-
- dirs = new Directory(directories[i]).list(filter, true);
- list = (String[])ArrayUtils.append(new String[list.length + dirs.length], list, dirs);
- }
- catch(IOException ex)
- {
- ex.printStackTrace();
- }
- }
- }
-
- return (list);
- }
-
- /**
- * @param filter TODO
- * @param dst TODO
- * @param recurse TODO
- * @since VCafe 3.0
- */
-
- public boolean copyTo(Directory dst, boolean recurse)
- throws IOException
- {
- return (copyTo(new DefaultFileFilenameFilter(), dst, recurse));
- }
-
- public boolean copyTo(FilenameFilter filter, Directory dst, boolean recurse)
- throws IOException
- {
- String targetPathName;
- String sourceRootPathName;
- String[] inputListing;
-
- if(getName().equals(dst.getName()))
- {
- return (false);
- }
-
- targetPathName = FileSystem.getCanonicalPath(dst.getName(), true);
- sourceRootPathName = FileSystem.getCanonicalPath(getName(), true);
-
- inputListing = list(filter, recurse);
-
- for(int i = 0; i < inputListing.length; i++)
- {
- String subdir;
- File sourceFile;
- String sourcePath;
- File targetFile;
-
- subdir = "";
- sourceFile = new File(inputListing[i]);
- sourcePath = FileSystem.getCanonicalPath(sourceFile.getParent(), true);
-
- // create new subdirectory if necessary
- if(!(sourceRootPathName.equals(sourcePath)))
- {
- subdir = sourcePath.substring(sourceRootPathName.length(), sourcePath.length());
- (new File(targetPathName + subdir)).mkdirs();
- }
-
- targetFile = new File(FileSystem.getCanonicalPath(targetPathName + subdir, true) + sourceFile.getName());
-
- if(!sourceFile.isDirectory())
- {
- if(!(copyFile(sourceFile, targetFile)))
- {
- return (false);
- }
- }
- else
- {
- System.out.println("** not copying directory " + sourceFile);
- }
- }
-
- return true;
- }
-
- /**
- * @param source TODO
- * @param target TODO
- * @exception java.io.IOException
- * @since VCafe 3.0
- */
- protected boolean copyFile(File source, File target)
- throws IOException
- {
- return (FileSystem.copyFile(source, target));
- }
-
- /**
- * @param filter TODO
- * @param dst TODO
- * @param recurse TODO
- * @since VCafe 3.0
- */
-
- public boolean moveTo(FilenameFilter filter, Directory dst, boolean recurse)
- {
- throw new com.symantec.itools.lang.NotImplementedError(com.symantec.itools.lang.Debug.getExecutionContext(2));
- }
-
- /**
- * @since VCafe 3.0
- */
-
- public boolean delete()
- {
- return (directory.delete());
- }
-
- public boolean deleteAll()
- {
- String[] list;
-
- list = listFiles(true);
-
- for(int i = 0; i < list.length; i++)
- {
- if(!(new File(list[i]).delete()))
- {
- return (false);
- }
- }
-
- list = listDirectories(true);
-
- for(int i = list.length - 1; i >= 0; i--)
- {
- if(!(new File(list[i]).delete()))
- {
- return (false);
- }
- }
-
- return (true);
- }
-
- /**
- * @since VCafe 3.0
- */
-
- public boolean canRead()
- {
- return (directory.canRead());
- }
-
- /**
- * @since VCafe 3.0
- */
-
- public boolean canWrite()
- {
- return (directory.canWrite());
- }
-
- /**
- * @since VCafe 3.0
- */
-
- public String getParent()
- {
- return (directory.getParent());
- }
-
- /**
- * @since VCafe 3.0
- */
-
- public long lastModified()
- {
- return (directory.lastModified());
- }
-
- /**
- * @param dir TODO
- * @since VCafe 3.0
- */
-
- public boolean renameTo(AbstractDirectory dir)
- {
- throw new com.symantec.itools.lang.NotImplementedError(com.symantec.itools.lang.Debug.getExecutionContext(2));
- }
-
- /**
- * @since VCafe 3.0
- */
- public boolean exists()
- {
- return (directory.exists());
- }
- }